import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import store from '@/store'
import Home from '@/views/menus/Home/Home.vue'
import userInfo from '@/views/menus/User/UserInfo'
import UserAvatar from '@/views/menus/User/UserAvatar'
import UserPwd from '@/views/menus/User/UserPwd'
import ArtCate from '@/views/menus/Article/ArtCate'
import ArtList from '@/views/menus/Article/ArtList'

Vue.use(VueRouter)

const routes = [
  { path: '/reg', component: Reg },
  { path: '/login', component: Login },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [{ path: 'home', component: Home },
      { path: 'user-info', component: userInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 设置白名单
const Array = ['/login', '/reg']

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (!token && !Array.includes(to.path)) {
    Vue.prototype.$message.error('请先登录')
    next('/login')
  } else {
    next()
  }
})
export default router
